package com.mengxin.secondkill.controller;

import com.google.common.hash.BloomFilter;
import com.mengxin.secondkill.annotation.Limit;
import com.mengxin.secondkill.message.req.AddGoodsReq;
import com.mengxin.secondkill.message.req.BuyGoodsReq;
import com.mengxin.secondkill.message.req.GoodsStockCacheReq;
import com.mengxin.secondkill.message.resp.AddGoodsResp;
import com.mengxin.secondkill.message.resp.BuyGoodsResp;
import com.mengxin.secondkill.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mengxin
 * @date 2020/12/11 17:44
 */
@Slf4j
@RestController()
@RequestMapping("/goods")
@Api(tags = "商品接口模块")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Limit(name = "goodsBuy",prefix = "secondkill:",key="goods:buy",period = 5,count = 3)
    @ApiOperation(value = "商品购买")
    @ApiImplicitParam(name = "req", value = "商品购买", required = true, dataType = "BuyGoodsReq")
    @PostMapping("buy")
    public BuyGoodsResp buyGoods(@RequestBody BuyGoodsReq req) throws InterruptedException {
        return goodsService.buyGoods(req);
    }

    @ApiOperation(value = "商品库存更新缓存")
    @ApiImplicitParam(name = "req", value = "商品库存更新缓存", required = true, dataType = "AddGoodsReq")
    @PostMapping("cache")
    public void goodsStockCache(@RequestBody GoodsStockCacheReq req){
        goodsService.goodsStockCache(req);
    }

    @ApiOperation(value = "商品新增")
    @ApiImplicitParam(name = "req", value = "商品购买", required = true, dataType = "AddGoodsReq")
    @PostMapping("add")
    public AddGoodsResp addGoods(@RequestBody AddGoodsReq req){
        return goodsService.addGoods(req);
    }
}
